<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\BShopGoods;
use Common\Models\Collects;
use Library\Validators\INArray;
use Services\Api\GoodsApiServices;

class CollectController extends ApiController
{
	public function initialize()
	{
		parent::initialize();
	}

    /**
     * 收藏商品
     */
    public function add_collectAction(){
        if(!isset($this->params['goods_id']) || empty($this->params['goods_id'])){
            $this->fail(-1,'商品id不能为空');
        }
        $data['product_id'] = $this->params['goods_id'];
        if(!isset($this->params['site']) || empty($this->params['site'])){
            $this->fail(-1,'站点不能为空');
        }else{
            if (!INArray::platformSite($this->params['site'])) {
                $this->fail(-1, '站点错误');
            }
        }
        $collectsDb = new Collects();
        $is_collects = $collectsDb->find_by_id($this->params['goods_id'],$this->params['site']);
        if($is_collects){
            $this->fail(-1,'该商品已经收藏');
        }

        $GoodsServices = new GoodsApiServices();
        $goods_detail = $GoodsServices->goods_detail($this->params['site'],$this->params['goods_id']);
        if(!$goods_detail['status']){
            $this->fail(-1,$goods_detail['msg']);
        }
        $res = $goods_detail['data'];
        if($this->params['site'] == 'self'){
            $BShopGoodsDb = new BShopGoods();
            $res = $BShopGoodsDb->get_goods_sn($this->params);
        }
        $collectsDb->user_id = $this->user_info['id'];
        $collectsDb->site = $this->params['site'];
        $collectsDb->product_id = $res['GoodsId'];
        $collectsDb->ShopType = $res['ShopType'];
        $collectsDb->goodsTitle = $res['ShortTitle'];
        $collectsDb->ShopName = $res['ShopName'];
        $collectsDb->ShortTitle = $res['ShortTitle'];
        $collectsDb->Price = $res['Price'];
        $collectsDb->PicLogo = $res['PicLogo'];
        $collectsDb->CouponMoney = $res['CouponMoney'];
        $collectsDb->MonthSale = $res['MonthSale'];
        $collectsDb->EndPrice = $res['EndPrice'];
        $collectsDb->CouponUrl = isset($res['Couponurl']) ? $res['Couponurl'] : '';
        $collectsDb->Commission = Common::bc($res['Commission'],'0','+');
        $collectsDb->MaxCommission = Common::bc($res['Commission'],'0','+');
        $collectsDb->created_at = date('Y-m-d H:i:s');
        if($collectsDb->save()){
            $this->ok([]);
        }else{
            $this->fail(-1,'操作失败');
        }
    }

    /**
     * 取消收藏商品
     */
    public function uncollectAction(){
        $collects = new Collects();
        if((isset($this->params['site']) && !empty($this->params['site'])) || (isset($this->params['goods_id']) && !empty($this->params['goods_id']))){
            $result = $collects->del_goodsSite_collect($this->params['goods_id'],$this->params['site'],$this->user_info['id']);
        }else{
            if(!isset($this->params['id']) || empty($this->params['id'])){
                $this->fail(-1,'收藏id不能为空');
            }
            $result = $collects->del_collect($this->user_info['id'],$this->params['id']);
        }
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'操作失败');
        }
    }

    /**
     * 收藏列表
     */
    public function collect_listAction(){
        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = Common::PAGE;
        }

        //当前条数
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size'] = Common::PAGESIZE;
        }
        $model = new Collects();
        $list = $model->getList($this->params);
        $this->ok($list);
    }
}
